package momo.multitree.simulation;

import momo.multitree.algo.EdgesMstSptValidator;
import momo.multitree.algo.EdmundAlgo;
import momo.multitree.algo.KrzyAlgo;
import momo.multitree.algo.LatencyShortestPath;
import momo.multitree.structure.*;
import momo.multitree.util.JackAlgo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SimulationWorker
{
	private Log log = LogFactory.getLog(SimulationWorker.class);
	
	private Graph orgGraph;
	private Tree bestLatTree, bestCostTree, bestStabTree;
	private JackAlgo algo;
	private GlobalOptTree globalOptTree;
	private DijkstraValidator validator;
	
	private EdgesMstSptValidator edgeValidator;
	
	public SimulationWorker(Graph g)
	{
		orgGraph = g;
		
//		LatencyDijkstra latTest = new LatencyDijkstra();
		LatencyShortestPath latTest = new LatencyShortestPath();
		bestLatTree = latTest.optimumTree(orgGraph);
		
//		CostDijkstra costTest = new CostDijkstra();
//		bestCostTree = costTest.optimumTree(orgGraph);
		KrzyAlgo costTest = new KrzyAlgo(orgGraph);
		bestCostTree = costTest.optimumTree();
		
//		StabilityOptCal stabTest = new StabilityOptCal();
//		bestStabTree = stabTest.optimumTree(orgGraph);
		
		EdmundAlgo stabTest = new EdmundAlgo(orgGraph);
		bestStabTree = stabTest.optimumTree();
		
//		System.out.println(bestStabTree.toString());
		
		algo = new JackAlgo();
		
		globalOptTree = new GlobalOptTree(bestLatTree, bestCostTree, bestStabTree);
		algo.addVisitor(globalOptTree);
		
		validator = new DijkstraValidator(bestLatTree, bestCostTree, bestStabTree, costTest.getOptimizer());
		algo.addVisitor(validator);
		
//		edgeValidator = new EdgesMstSptValidator(g);
//		algo.addVisitor(edgeValidator);
	}
	
	public String run()
	{
		algo.generateSpanningGraph(orgGraph);
//		edgeValidator.validateOnBestTree();
//		return globalOptTree.toString() +", " + validator.toString();
		return globalOptTree.toString();
	}
	
	public void runSimulation()
	{
		algo.generateSpanningGraph(orgGraph);
	}
	
	public Tree getBestTree()
	{
		return globalOptTree.getBestTree();
	}
}//end of class SimulationWorker
